package org.example.trigger.http;


import lombok.extern.slf4j.Slf4j;
import org.example.api.response.Response;
import org.example.types.enums.ResponseCode;
import org.redisson.api.RTopic;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author yinghuashen
 * @version 1.0
 * @description
 * @date 2025/3/8 14:47
 */

@Slf4j
@RestController
@CrossOrigin("*")
@RequestMapping("/dcc")
public class DCCController {

    @Resource
    private RTopic dccTopic;

    @RequestMapping("/update_config")
    public Response<String> updateDCC(@RequestParam String key, @RequestParam String value) {
        try {
            dccTopic.publish(key + ":" + value);
            return Response.<String>builder()
                    .code(ResponseCode.SUCCESS.getCode()).info(ResponseCode.SUCCESS.getInfo()).data(null).build();

        } catch (Exception e) {
            log.error("更新DCC失败", e);
            return Response.<String>builder()
                    .code(ResponseCode.DYNAMIC_CONFIG_UPDATE_ERROR.getCode())
                    .info(ResponseCode.DYNAMIC_CONFIG_UPDATE_ERROR.getInfo())
                    .data(null).build();
        }
    }
}
